1 /*
2 * $Header: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/XMLBeanInfo.java,v 1.4 2002/09/26 19:29:33 rdonkin Exp $
3 * $Revision: 1.4 $
4 * $Date: 2002/09/26 19:29:33 $
5 *
6 * ====================================================================
7 *
8 * The Apache Software License, Version 1.1
9 *
10 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
11 * reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 *
25 * 3. The end-user documentation included with the redistribution, if
26 * any, must include the following acknowlegement:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowlegement may appear in the software itself,
30 * if and wherever such third-party acknowlegements normally appear.
31 *
32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33 * Foundation" must not be used to endorse or promote products derived
34 * from this software without prior written permission. For written
35 * permission, please contact apache@apache.org.
36 *
37 * 5. Products derived from this software may not be called "Apache"
38 * nor may "Apache" appear in their names without prior written
39 * permission of the Apache Group.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
59 *
60 * $Id: XMLBeanInfo.java,v 1.4 2002/09/26 19:29:33 rdonkin Exp $
61 */
62 package org.apache.commons.betwixt;
63
64 /*** <p><code>XMLBeanInfo</code> represents the XML metadata information
65 * used to map a Java Bean cleanly to XML. This provides a default
66 * introspection mechansim, rather like {@link java.beans.BeanInfo}
67 * which can be customized through some mechansim, either via Java code
68 * or XSLT for example.</p>
69 *
70 * <h4><code>ID</code> and <code>IDREF</code> Attribute Names</h4>
71 * <p>These special attributes are defined in the xml specification.
72 * They are used by Betwixt to write bean graphs with cyclic references.
73 * In most cases, these will take the values 'id' and 'idref' respectively
74 * but these names can be varied in the DTD.
75 * Therefore, these names are specified by this class but default to the
76 * usual values.</p>
77 *
78 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
79 * @version $Revision: 1.4 $
80 */
81 public class XMLBeanInfo {
82 /*** Descriptor for main element */
83 private ElementDescriptor elementDescriptor;
84
85 /*** the beans class that this XML info refers to */
86 private Class beanClass;
87 /*** <code>ID</code> attribute name */
88 private String idAttributeName = "id";
89 /*** <code>IDREF</code> attribute name */
90 private String idrefAttributeName = "idref";
91 /*** Have we already cached the <code>idAttributeDescriptor</code>? */
92 private boolean cachedIDAttribute = false;
93 /*** Cached <code>ID</code> attribute descriptor */
94 private AttributeDescriptor idAttributeDescriptor;
95
96 /*** Base constructor */
97 public XMLBeanInfo( Class beanClass ) {
98 this.beanClass = beanClass;
99 }
100
101 /*** Get descriptor for bean represention */
102 public ElementDescriptor getElementDescriptor() {
103 return elementDescriptor;
104 }
105
106 /*** Set descriptor for bean represention */
107 public void setElementDescriptor(ElementDescriptor elementDescriptor) {
108 this.elementDescriptor = elementDescriptor;
109 }
110
111 /***
112 * Gets the beans class that this XML info refers to
113 *
114 * @return the beans class that this XML info refers to
115 */
116 public Class getBeanClass() {
117 return beanClass;
118 }
119
120 /*** Sets the beans class that this XML info refers to */
121 public void setBeanClass(Class beanClass) {
122 this.beanClass = beanClass;
123 }
124
125 /*** Search attributes for one matching <code>ID</code> attribute name */
126 public AttributeDescriptor getIDAttribute() {
127 //
128 // XXX for some reason caching isn't working at the moment
129 // it could be that this method is called too early
130 // and not reset when attributes change
131 // on the other hand, the speed gain is likely to be too
132 // small to bother about
133 //
134 //if ( cachedIDAttribute = false ) {
135 idAttributeDescriptor = findIDAttribute();
136 // cachedIDAttribute = true;
137 //}
138 return idAttributeDescriptor;
139 }
140
141 /*** ID attribute search implementation */
142 private AttributeDescriptor findIDAttribute() {
143 // we'll check to see if the bean already has an id
144 if ( getElementDescriptor().hasAttributes() ) {
145 AttributeDescriptor[] attributes = getElementDescriptor().getAttributeDescriptors();
146 if ( attributes != null ) {
147 for ( int i = 0, size = attributes.length; i < size; i++ ) {
148 // support a match either on local or qualified name
149 if ( getIDAttributeName().equals( attributes[i].getQualifiedName() )
150 || getIDAttributeName().equals( attributes[i].getLocalName() )) {
151 // we've got a match so use this attribute
152 return attributes[i];
153
154 }
155 }
156 }
157 }
158 return null;
159 }
160
161 /***
162 * <p>Get name of <code>ID</code> attribute.
163 * This is used to write (for example) automatic <code>ID</code>
164 * attribute values.</p>
165 *
166 * <p>The default name is 'id'.</p>
167 *
168 * @return name for the special <code>ID</code> attribute
169 */
170 public String getIDAttributeName() {
171 return idAttributeName;
172 }
173 /***
174 * Set name of <code>ID</code> attribute
175 * This is used to write (for example) automatic <code>ID</code>
176 * attribute values.</p>
177 *
178 * <p>The default name is 'id'.</p>
179 *
180 * @param idAttributeName the attribute name for the special <code>ID</code> attribute
181 */
182 public void setIDAttributeName(String idAttributeName) {
183 this.idAttributeName = idAttributeName;
184 }
185
186 /***
187 * <p>Get <code>IDREF</code> attribute name
188 * This is used (for example) to deal with cyclic references.
189 *
190 * <p>The default name is 'idref'.</p>
191 *
192 * @return name for the special <code>IDREF</code> attribute
193 */
194 public String getIDREFAttributeName() {
195 return idrefAttributeName;
196 }
197
198 /***
199 * Set <code>IDREF</code> attribute name
200 * This is used (for example) to deal with cyclic references.
201 *
202 * <p>The default name is 'idref'.</p>
203 *
204 * @param idrefAttributeName the attribute name for the special <code>IDREF</code> attribute
205 */
206 public void setIDREFAttributeName(String idrefAttributeName) {
207 this.idrefAttributeName = idrefAttributeName;
208 }
209
210 public String toString() {
211 return
212 "XMLBeanInfo [class=" + getBeanClass()
213 + ", descriptor=" + getElementDescriptor() + "]";
214 }
215 }
This page was automatically generated by Maven